package com.mercenary.wein.screenControl.deviceControll;

import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.IDevice;
import com.mercenary.wein.screenControl.ui.util.Constants;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by tuxiao on 2017/2/26.
 */
public class DevicesDetector {
    private static DevicesDetector instance;
    private DeviceConnectedListener connectedListener;
    private DeviceDisConnectedListener disConnectedListener;


    public DeviceDisConnectedListener getDisConnectedListener() {
        return disConnectedListener;
    }

    public void setDisConnectedListener(DeviceDisConnectedListener disConnectedListener) {
        this.disConnectedListener = disConnectedListener;
    }

    public DeviceConnectedListener getConnectedListener() {
        return connectedListener;
    }

    public void setConnectedListener(DeviceConnectedListener connectedListener) {
        this.connectedListener = connectedListener;
    }

    private DevicesDetector(){

    }
    AndroidDebugBridge bridge = null;

    public Map<String, AndroidDevice> getDevicesMap() {
        return devicesMap;
    }

    public void setDevicesMap(Map<String, AndroidDevice> devicesMap) {
        this.devicesMap = devicesMap;
    }

    Map<String, AndroidDevice> devicesMap = null;

    public static DevicesDetector getInstance(){
        synchronized (DevicesDetector.class){
            if(instance==null){
                instance = new DevicesDetector();
                instance.init();
                return instance;
            }else {
                return instance;
            }
        }
    }

    public interface DeviceConnectedListener{
        public void handler(AndroidDevice androidDevice);
    }

    public interface DeviceDisConnectedListener{
        public void handler(AndroidDevice androidDevice);
    }

    public void init(){
        devicesMap = new HashMap<String, AndroidDevice>();

        AndroidDebugBridge.init(false);
        if(Constants.ADBFilePath==null||Constants.ADBFilePath.length()==0){
            bridge = AndroidDebugBridge.createBridge();
        }else {
            bridge = AndroidDebugBridge.createBridge(Constants.ADBFilePath,true);
        }
        waitDeviceList(bridge);
        AndroidDebugBridge.addDeviceChangeListener(new AndroidDebugBridge.IDeviceChangeListener() {
            public void deviceChanged(IDevice device, int arg1) {
                // not implement
            }
            public void deviceConnected(IDevice device) {
                AndroidDevice androidDevice = new AndroidDevice(device);
                devicesMap.put(device.getSerialNumber(),androidDevice);
                if(connectedListener!=null)connectedListener.handler(androidDevice);
                System.out.println(String.format("%s connected", device.getSerialNumber()));
            }
            public void deviceDisconnected(IDevice device) {
                AndroidDevice androidDevice = devicesMap.get(device.getSerialNumber());
                devicesMap.remove(androidDevice);
                if(connectedListener!=null)disConnectedListener.handler(androidDevice);
                System.out.println(String.format("%s disconnected", device.getSerialNumber()));
            }
        });
        for(IDevice device:bridge.getDevices()){
            AndroidDevice androidDevice = new AndroidDevice(device);
            devicesMap.put(androidDevice.getSerialNo(),androidDevice);
        }

    }

    public AndroidDebugBridge getBridge() {
        return bridge;
    }

    public void setBridge(AndroidDebugBridge bridge) {
        this.bridge = bridge;
    }

    public static void main(String[] args) throws IOException {
        new DevicesDetector().init();
        System.in.read();
    }

    private void waitDeviceList(AndroidDebugBridge bridge) {
        int count = 0;
        while (bridge.hasInitialDeviceList() == false) {
            try {
                Thread.sleep(100);
                count++;
            } catch (InterruptedException e) {
                // pass
            }
            // let's not wait > 10 sec.
            if (count > 300) {
                throw new RuntimeException("Timeout getting device list!");
            }
        }
    }
}

